﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DotNet.WebUI.Admin
{
    public partial class UsersList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["Category"] = GetQueryString("Category", "0");

                FillData();
            }
        }

        private void FillData()
        {
            int pageIndex = pagData.CurrentPageIndex;
            int pageSize = pagData.PageSize;
            string _Sql = @"SELECT Count(*) 
                FROM Users 
                WHERE LoginID & UserName like '%{0}%' ";
            _Sql = string.Format(_Sql, txtLoginID.Text);

            pagData.RecordCount = Convert.ToInt32(ExecuteScalar(_Sql));

            _Sql = @"SELECT * 
                FROM Users
                WHERE LoginID & UserName like '%{0}%' 
                ORDER BY LoginID ";
            _Sql = string.Format(_Sql, txtLoginID.Text);

            rptData.DataSource = ExecuteDataTable(_Sql, pageIndex, pageSize);
            rptData.DataBind();
        }

        protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName.ToLower())
            {
                case "edit":
                    Response.Redirect(string.Format("UsersEdit.aspx?Operate=Edit&LoginID={0}", e.CommandArgument));
                    break;
                case "del":
                    string sql = @"DELETE FROM Users
                        WHERE LoginID = '{0}' ";
                    sql = string.Format(sql, e.CommandArgument.ToString());
                    ExecuteNonQuery(sql);
                    break;
                case "pwd":
                    ExecuteNonQuery(string.Format(@"UPDATE Users Set Pwd ='{1}'
                        WHERE LoginID = '{0}' ", e.CommandArgument.ToString(), "123456"));

                    break;
                case "purview":
                    Response.Redirect(string.Format("Purview.aspx?LoginID={0}", e.CommandArgument));
                    break;
            }
            FillData();
        }

        protected void pagData_PageChanged(object sender, EventArgs e)
        {
            FillData();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("UsersEdit.aspx?Operate=New");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            FillData();
        }
    }
}